package com.yt.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

/**
 * @Description:
 * @Author: mxli
 * @Date: 2021-01-12 22:02
 **/
public class UeditorUtils {

    public static void main(String[] args) {
//        String content = "<p><br></p><p><img style=\"max-width:100%;\" src=\"/group1/M01/00/2A/rBsACF_RwJKAAwwKAAuOQMukT88688.png\"></p><p><img style=\"max-width:100%;\" src=\"/group1/M02/00/2A/rBsACF_RwL6ASyy3ABHTRAYIXWU054.png\"></p><p><img style=\"max-width:100%;\" src=\"/group1/M02/00/2A/rBsACF_RwWCAfcsBAA-gCJiTTEQ910.png\"></p><p><br></p>";
        String content = "<p>下载地址：<a title=\"持续更新补丁下载地址\" target=\"_blank\" href=\"http://pan.baidu.com/s/xxxx\">持续更新补丁下载地址</a></p><p>";
        List<String> imgSrcList = getImgStr(content);
        for (String imgSrc : imgSrcList){
            System.out.println("----"+ imgSrc);
        }
        List<String> ahrefList = getAhhrefStr(content);
        for (String href : ahrefList){
            System.out.println("----"+ href);
        }
    }

    public static List<String> getImgStr(String htmlStr) {
        List<String> list = new ArrayList<>();
        if(StrUtils.isEmpty(htmlStr)){
            return list;
        }
        String img = "";
        Pattern p_image;
        Matcher m_image;
        // String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            // 得到<img />数据
            img = m_image.group();
            // 匹配<img>中的src数据
            Matcher m = compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                list.add(m.group(1));
            }
        }
        return list;
    }

    public static List<String> getAhhrefStr(String htmlStr) {
        List<String> list = new ArrayList<>();
        String img = "";
        Pattern p_image;
        Matcher m_image;
        // String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<a.*href\\s*=\\s*(.*?)[^>]*?>";
        p_image = compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            // 得到<img />数据
            img = m_image.group();
            // 匹配<img>中的src数据
            Matcher m = compile("href\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                list.add(m.group(1));
            }
        }
        return list;
    }
}
